#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>

char drvName[]="proc sample";
int val_a, val_b;

static int proc_sample_write(struct file *file, const char *buf, 
	unsigned long count, void *data);

static int proc_sample_read(char *buf, char **start, off_t offset, 
		int count, int *eof, void *data);

struct proc_dir_entry * proc_pdc_root = NULL;
struct proc_dir_entry * ent;


static int hello_init(void)
{
	printk(KERN_ALERT " Hello world enter!\n");
	
	proc_pdc_root = proc_mkdir("proc_sample", 0);
	ent = create_proc_entry("val", 00666, proc_pdc_root);
	ent->data = drvName;
	ent->read_proc = proc_sample_read;
	ent->write_proc = proc_sample_write;
	
	return 0;
}

static void hello_exit(void)
{
	remove_proc_entry("val", proc_pdc_root);
	remove_proc_entry("proc_sample", 0);
	printk(KERN_ALERT " Hello world exit!\n");
}

static int proc_sample_write(struct file *file, const char *buf, 
	unsigned long count, void *data)
{
	ssize_t result;
	char lbuf[count + 1];
	
	memset(lbuf, 0, count + 1);

	if (copy_from_user(lbuf, buf, count))
		return -EFAULT;
	result = sscanf(lbuf, "%d %d", &val_a, &val_b);
	printk(KERN_ALERT "[%s]:%d %d %d\n", __func__, val_a, val_b, result);

	return count;
}


static int proc_sample_read(char *page, char **start, off_t off, 
		int count, int *eof, void *data)
{
	char *out = page;
	int len;

	printk(KERN_ERR "This function maybe not correct!\n");

	switch ((long)data)
	{
	case 1:
		out += sprintf(out, "Heartbeat: %d\n", led_heartbeat);
		out += sprintf(out, "LAN Rx/Tx: %d\n", led_lanrxtx);
		break;
	case 2:
		out += sprintf(out, "%s\n", lcd_text);
		break;
	default:
		*eof = 1;
		return 0;
	}

	len = out - page - off;
	if (len < count) {
		*eof = 1;
		if (len <= 0) return 0;
	} else {
		len = count;
	}
	*start = page + off;
	return len;
}


module_init(hello_init);
module_exit(hello_exit);
